package com.dayuanit.dy17.mix.current;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TestDeadLock {
    private static final Object lock1 = new Object();
    private static final Object lock2 = new Object();

    private static final Lock lockA = new ReentrantLock();
    private static final Lock lockB = new ReentrantLock();

    public static void a() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock1) {
                    System.out.println(Thread.currentThread().getName() + " lock1");
                    synchronized (lock2) {
                        System.out.println(Thread.currentThread().getName() + " lock2");
                    }
                }
            }
        }, "t1").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock2) {
                    System.out.println(Thread.currentThread().getName() + " lock2");
                    synchronized (lock1) {
                        System.out.println(Thread.currentThread().getName() + " lock1");
                    }
                }
            }
        }, "t2").start();
    }

    public static void b() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                lockA.lock();
                try {
                    System.out.println(Thread.currentThread().getName() + " lockA");

                    try {
                        if (!lockB.tryLock(3, TimeUnit.SECONDS)) {
                            System.out.println(Thread.currentThread().getName() + " 等待超时...");
                            return;
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    try {
                        System.out.println(Thread.currentThread().getName() + " lockB");
                    } finally {
                        lockB.unlock();
                    }
                } finally {
                    lockA.unlock();
                }
            }
        }, "t1").start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                lockB.lock();
                try {
                    System.out.println(Thread.currentThread().getName() + " lockB");

                    try {
                        if (!lockA.tryLock(3, TimeUnit.SECONDS)) {
                            System.out.println(Thread.currentThread().getName() + " 等待超时...");
                            return;
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    try {
                        System.out.println(Thread.currentThread().getName() + " lockA");
                    } finally {
                        lockB.unlock();
                    }
                } finally {
                    lockA.unlock();
                }
            }
        }, "t2").start();

    }


    public static void main(String[] args) {
//       a();
       b();
    }

}
